/*
 * Copyright (c) 2006-2020, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020-03-16     Ghazi       the first version
 */
#include "lvglrtt.h"
#include "rtthread.h"

#define DBG_TAG "lvgl.rtt"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

#define BSP_LCD_LVGL

#ifdef BSP_LCD_LVGL
#include "usr_api.h"
#include "cJSON.h"
#include "webclient.h"

extern void lv_port_disp_init(void);
extern void lv_port_indev_init(void);

static  char title_buff[20] = {0};

static lv_obj_t *label_title = RT_NULL;

#ifdef BSP_USING_CAMERA
struct drv_camera_device
{
    struct rt_device parent;

    struct rt_semaphore camera_sem;
    struct rt_mutex camera_lock;

    struct rt_device_graphic_info camera_info;

    struct rt_i2c_bus_device  * i2c_device;

    DCMI_HandleTypeDef dcmi;

    DMA_HandleTypeDef hdma_dcmi;
};

extern struct drv_camera_device _camera;
uint8_t lvgl_camrea;

lv_img_dsc_t camera_img = {
  .header.always_zero = 0,
  .header.w = LCD_WIDTH,
  .header.h = LCD_HEIGHT,
  .data_size = LCD_WIDTH * LCD_HEIGHT * LCD_BITS_PER_PIXEL / 8,
  .header.cf = LV_IMG_CF_TRUE_COLOR,
};

static lv_obj_t *camera_img_obj = RT_NULL;

void reflash_camera(void)
{
    if(camera_img_obj != RT_NULL)
        lv_img_set_src(camera_img_obj, &camera_img);
}

#else

struct weather
{
    lv_obj_t * cont_weather;
    lv_obj_t * lab_info;
    lv_obj_t * lab_weather;
    lv_obj_t * img_weather;
}weather;

static void realtime_handler(void *parameter)
{
    time_t now_value = time(RT_NULL);
    struct tm * now = gmtime(&now_value);
    rt_snprintf(title_buff, sizeof(title_buff) - 1, LV_SYMBOL_WIFI LV_SYMBOL_BATTERY_FULL "%02d:%02d", now->tm_hour, now->tm_min);
    lv_label_set_static_text(label_title, title_buff);
}

static void weather_handler(void *parameter)
{
    char * response_str = RT_NULL;
    char info[200] = {0};
    char buff[50] = {0};

    response_str = http_weather_get(RT_NULL);
    if(response_str != RT_NULL)
    {
        cJSON *pRcvJson = cJSON_Parse((const char *)response_str);
        if(pRcvJson != RT_NULL)
        {
            cJSON *item = cJSON_GetObjectItem(pRcvJson,"error_code");//error_code
            if(item != RT_NULL && item->valueint == 0)//error_code == 0
            {
                cJSON *result = cJSON_GetObjectItem(pRcvJson,"result");//result
                if(result != RT_NULL)
                {
                    if(weather.cont_weather != RT_NULL)
                        lv_obj_del(weather.cont_weather);
                    item = cJSON_GetObjectItem(result,"city");//city
                    if(item != RT_NULL)
                    {
                        weather.cont_weather = lv_cont_create(lv_scr_act(), RT_NULL);
                        lv_obj_set_size(weather.cont_weather, lv_disp_get_hor_res(RT_NULL), lv_disp_get_ver_res(RT_NULL));
                        lv_obj_set_pos(weather.cont_weather, 0, 0);
                        weather.lab_weather = lv_label_create(weather.cont_weather, NULL);
                        lv_label_set_text(weather.lab_weather, item->valuestring);
                        lv_obj_align(weather.lab_weather, weather.cont_weather, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
                    }
                    cJSON *realtime = cJSON_GetObjectItem(result,"realtime");//realtime
                    if(realtime != RT_NULL)
                    {
                        weather.lab_info = lv_label_create(weather.cont_weather, NULL);
                        weather.img_weather = lv_img_create(weather.cont_weather, NULL);

                        item = cJSON_GetObjectItem(realtime,"temperature");//temperature
                        rt_snprintf(buff, sizeof(buff) - 1, "temp:%s\n", item->valuestring);
                        strcat(info, buff);

                        item = cJSON_GetObjectItem(realtime,"humidity");//humidity
                        rt_snprintf(buff, sizeof(buff) - 1, "humy:%s\n", item->valuestring);
                        strcat(info, buff);

                        item = cJSON_GetObjectItem(realtime,"info");//info
                        if(rt_strcmp(item->valuestring, "晴") == 0)
                            lv_img_set_src(weather.img_weather, "weather/qing.png");
                        else if(rt_strcmp(item->valuestring, "多云") == 0)
                            lv_img_set_src(weather.img_weather, "weather/duoyun.png");
                        lv_obj_align(weather.img_weather, weather.cont_weather, LV_ALIGN_IN_LEFT_MID, 0, 0);

                        item = cJSON_GetObjectItem(realtime,"wid");//wid
                        rt_snprintf(buff, sizeof(buff) - 1, "wid:%s\n", item->valuestring);
                        strcat(info, buff);

                        item = cJSON_GetObjectItem(realtime,"direct");//direct
                        rt_snprintf(buff, sizeof(buff) - 1, "direct:%s\n", item->valuestring);
                        strcat(info, buff);

                        item = cJSON_GetObjectItem(realtime,"power");//power
                        rt_snprintf(buff, sizeof(buff) - 1, "power:%s\n", item->valuestring);
                        strcat(info, buff);

                        item = cJSON_GetObjectItem(realtime,"aqi");//aqi
                        rt_snprintf(buff, sizeof(buff) - 1, "aqi:%s\n", item->valuestring);
                        strcat(info, buff);
                        lv_label_set_text(weather.lab_info, info);
                        lv_obj_set_size(weather.lab_info, 64, lv_obj_get_height(weather.cont_weather));
                        lv_obj_align(weather.lab_info, weather.cont_weather, LV_ALIGN_IN_TOP_RIGHT, 0, 0);
                    }
                    cJSON *future = cJSON_GetObjectItem(result,"future");//future
                    if(future != RT_NULL)
                    {
                        int days = cJSON_GetArraySize(future);
                        for(int i = 0;i < days;i ++)
                        {
                            cJSON *future_iedex = cJSON_GetArrayItem(future,i);
                            if(future_iedex != RT_NULL)
                            {
                                item = cJSON_GetObjectItem(future_iedex,"date");//date
                                //item->valuestring
                                item = cJSON_GetObjectItem(future_iedex,"temperature");//temperature
                                //item->valuestring
                                item = cJSON_GetObjectItem(future_iedex,"weather");//weather
                                //item->valuestring
                                cJSON *wid = cJSON_GetObjectItem(future_iedex,"wid");//wid
                                if(wid != RT_NULL)
                                {
                                    item = cJSON_GetObjectItem(wid,"day");//day
                                    //item->valuestring
                                    item = cJSON_GetObjectItem(wid,"night");//night
                                    //item->valuestring
                                }
                                item = cJSON_GetObjectItem(future_iedex,"direct");//direct
                                //item->valuestring
                            }
                        }
                    }
                }
            }
            else
            {
            //��ȡʧ��
            }
            if(pRcvJson != RT_NULL)
                cJSON_Delete(pRcvJson);
        }
        web_free(response_str);
    }
}
MSH_CMD_EXPORT(weather_handler, weather);
#endif

static void lvglrtt_interface_entry(void *parameter)
{
#ifdef BSP_USING_CAMERA
        camera_img.data = (const uint8_t * )_camera.camera_info.framebuffer;
        camera_img_obj = lv_img_create(lv_scr_act(), NULL);
        lv_img_set_src(camera_img_obj, &camera_img);
#else
        time_t now_value = time(RT_NULL);
        struct tm * now = gmtime(&now_value);
        rt_snprintf(title_buff, sizeof(title_buff) - 1, LV_SYMBOL_WIFI LV_SYMBOL_BATTERY_FULL "%02d:%02d", now->tm_hour, now->tm_min);

        label_title = lv_label_create(lv_scr_act(), NULL);
        lv_label_set_long_mode(label_title, LV_LABEL_LONG_CROP);
        lv_label_set_align(label_title, LV_LABEL_ALIGN_RIGHT);
        lv_label_set_static_text(label_title, title_buff);
        lv_obj_set_size(label_title, lv_disp_get_hor_res(NULL), 16);

        lv_obj_t *text_label =  lv_label_create(lv_scr_act(), NULL);
        lv_label_set_recolor(text_label, true);
        lv_obj_align(text_label, label_title, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 4);
        lv_label_set_text(text_label, "Hello I am Ghazigq!\n#ff0000 LittlevGl2RTT V6.1.2#");

        png_obj = lv_img_create(lv_scr_act(), NULL);
        lv_img_set_src(png_obj, &camera_img);
        lv_obj_align(png_obj, text_label, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);

        rt_timer_t timer = rt_timer_create("realtime",realtime_handler,RT_NULL,200, RT_TIMER_FLAG_SOFT_TIMER|RT_TIMER_FLAG_PERIODIC);
        if(RT_NULL != timer);
            rt_timer_start(timer);

        timer = rt_timer_create("weather",weather_handler,RT_NULL,3600*1000, RT_TIMER_FLAG_SOFT_TIMER|RT_TIMER_FLAG_PERIODIC);
        if(RT_NULL != timer);
            rt_timer_start(timer);
#endif
    while (1)
    {
        lv_task_handler();
        rt_thread_mdelay(10);
    }
}

static void tick_handler(void *parameter)
{
    lv_tick_inc(10);
}

int lvglrtt_init(void)
{
    lv_init();
    lv_port_disp_init();
//    lv_port_indev_init();
    png_decoder_init();

    rt_timer_t timer = rt_timer_create("lvglrtt",tick_handler,RT_NULL,10, RT_TIMER_FLAG_HARD_TIMER|RT_TIMER_FLAG_PERIODIC);
    RT_ASSERT(RT_NULL != timer);
    rt_timer_start(timer);

    rt_thread_t tid = rt_thread_create("lvglrtt", lvglrtt_interface_entry, RT_NULL, 4096, 29, 10);
    RT_ASSERT(RT_NULL != tid);
    rt_thread_startup(tid);

    return 0;
}
INIT_APP_EXPORT(lvglrtt_init);

#endif /* BSP_LCD_LVGL */

